package com.feel.mall.fileservice.controller;

import cn.hutool.core.util.IdUtil;
import com.feel.mall.common.bean.RespBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @Author: 刘永钦
 * @Description:
 */
@RestController
@RequestMapping("/file")
public class FileController {
    @Value("${images.path}")
    private String basePath;

    @PostMapping("/upload")
    public RespBean upload(MultipartFile file) throws IOException {
        String name = file.getOriginalFilename();
        // 雪花算法重命名
        String strSnow = IdUtil.getSnowflakeNextIdStr();
        String new_name = strSnow + name.substring(name.lastIndexOf("."));

        file.transferTo(new File(basePath + new_name));
        return RespBean.ok("上传成功", new_name);
    }
}
